package com.ctvit.sjk.knowledge.problem.controller;

import com.ctvit.sjk.knowledge.core.controller.KnowledgeController;
import com.ctvit.sjk.knowledge.core.page.TableRecordInfo;
import com.ctvit.sjk.knowledge.problem.pojo.dto.ProblemQueryDTO;
import com.ctvit.sjk.knowledge.problem.service.ITabProblemService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * 问题Controller
 *
 * @author heyingcheng
 * @date 2025/3/22
 */
@RestController
@RequestMapping("knowledge/dataset")
public class TabProblemController extends KnowledgeController {

    private final ITabProblemService tabProblemService;

    public TabProblemController(ITabProblemService tabProblemService) {
        this.tabProblemService = tabProblemService;
    }

    /**
     * 获取问题列表
     */
    @GetMapping("{datasetId}/problem/{pageNum}/{pageSize}")
    public TableRecordInfo list(@PathVariable String datasetId,
                                        @PathVariable int pageNum,
                                        @PathVariable int pageSize,
                                        @RequestParam(value = "content", required = false) String content) {
        ProblemQueryDTO dto = new ProblemQueryDTO(content);

        startPage(pageNum, pageSize);
        return getRecordTable(tabProblemService.list(datasetId, dto), pageNum, pageSize);
    }

    /**
     * 批量创建问题
     */
    @Log(title = "问题", businessType = BusinessType.INSERT)
    @PostMapping("{datasetId}/problem")
    public AjaxResult batchCreate(@PathVariable String datasetId, @RequestBody List<String> contentList) {
        return success(tabProblemService.batchCreate(datasetId, contentList, CREATE_ID));
    }

    /**
     * 批量删除问题
     */
    @Log(title = "问题", businessType = BusinessType.DELETE)
    @DeleteMapping("{datasetId}/problem/_batch")
    public AjaxResult batchRemove(@PathVariable String datasetId, @RequestBody List<String> problemIdList) {
        return toAjax(tabProblemService.batchRemove(datasetId, problemIdList, CREATE_ID));
    }

    /**
     * 删除问题
     */
    @Log(title = "问题", businessType = BusinessType.DELETE)
    @DeleteMapping("{datasetId}/problem/{problemId}")
    public AjaxResult remove(@PathVariable String datasetId, @PathVariable String problemId) {
        return toAjax(tabProblemService.batchRemove(datasetId, Collections.singletonList(problemId), CREATE_ID));
    }

}
